package com.zw.leave.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.zw.commons.log.annotation.Log;
import com.zw.commons.log.enums.BusinessType;
import com.zw.commons.utlis.JwtHelper;
import com.zw.leave.service.LeaveApplyService;
import com.zw.object.leave.LeaveApply;
import com.zw.object.result.Result;
import com.zw.object.system.SysUser;
import com.zw.object.vo.SearchLeaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;

/**
 * <p>
 * 请假审批表 前端控制器
 * </p>
 *
 * @author zw
 * @since 2023-03-10
 */
@RestController
@RequestMapping("/leave/leave-apply")
@Api(tags = "请假管理接口")
public class LeaveApplyController {

    @Autowired
    private LeaveApplyService leaveApplyService;

    @PostMapping("/save")
    @ApiOperation("新增请假单")
    @Log(title = "新增请假单",businessType = BusinessType.INSERT)
    public Result saveLeave(@RequestBody LeaveApply leaveApply, HttpServletRequest request){
        String token = request.getHeader("token");
        String username = JwtHelper.getUsername(token);
        leaveApply.setUsername(username);
        leaveApply.setStatus(1);
        leaveApply.setDeptId(1L);
        leaveApply.setId(JwtHelper.getUserId(token));
        leaveApply.setNickname(JwtHelper.getNickName(token));
        return leaveApplyService.saveLeaver(leaveApply);
    }

    @PostMapping("/list")
    @ApiOperation("获取请假信息")
    @Log(title = "获取请假信息",businessType = BusinessType.SELECT)
    public Result leaveListAndPage(@RequestBody SearchLeaveVo leaveVo,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JwtHelper.getUserId(token);
        return leaveApplyService.searchLeaveAndPage(leaveVo,userId);
    }

    @PostMapping("/update")
    @ApiOperation("请假信息修改")
    @Log(title = "请假信息修改",businessType = BusinessType.UPDATE)
    public Result updateById(@Validated @RequestBody LeaveApply leaveApply){
        return leaveApplyService.updateLeaveInfoById(leaveApply);
    }
}

